//
//  RSNavigationController.swift
//  WeiBoTest
//
//  Created by 任帅 on 2017/2/23.
//  Copyright © 2017年 RENSHUAI. All rights reserved.
//

import UIKit

class RSNavigationController: UINavigationController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //隐藏默认的NavigationBar
        navigationBar.isHidden = true
    }

    //重写push方法，所有的push动作都会调用此方法
    //viewController是被push的控制器，设置他左侧的按钮为返回按钮
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        //如果不是栈低控制器才需要隐藏，根控制器不需要处理
        if childViewControllers.count > 0 {
            
            viewController.hidesBottomBarWhenPushed = true
            //判断控制器的类型
            if let vc = viewController as? RSBaseViewController {
                
                var title = "返回"
                // 判断控制器级数
                if childViewControllers.count == 1 {
                
                    title = childViewControllers.first?.title ?? "返回"
                    
                }
                
                //取出自定义navItem,设置左侧按钮作为返回按钮
                vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(popBack),isBackButton:true)
                
            }
        }
        
        super.pushViewController(viewController, animated: true)
        
    }
    @objc private func popBack(){
    
        popViewController(animated: true)
    }
   

}
